import os


def rename_files_in_directory(directory):
    # 确保提供的路径是一个文件夹
    if not os.path.isdir(directory):
        print("提供的路径不是有效的文件夹。")
        return

    # 获取文件夹内所有文件
    files = os.listdir(directory)
    # 过滤出所有的 jpg 文件
    jpg_files = [f for f in files if f.endswith('.jpg')]
    # 错误文件集合
    err_tag = 0

    if 'e.jpg' in jpg_files:
        err_tag = 1
        print("Error folder: {0}".format(directory))

    if '01.jpg' in jpg_files:
        # 找到 01.jpg，准备重命名其他文件
        base_number = 2
        for file in jpg_files:
            if file != '01.jpg':
                new_name = f"{base_number:02}.jpg"
                os.rename(os.path.join(directory, file), os.path.join(directory, new_name))
                print("renamed ", os.path.join(directory, file), "->", os.path.join(directory, new_name))
                base_number += 1
    else:
        # 如果没有 01.jpg，按 01.jpg 开始命名
        base_number = 1
        for file in jpg_files:
            new_name = f"{base_number:02}.jpg"
            os.rename(os.path.join(directory, file), os.path.join(directory, new_name))
            print("renamed ", os.path.join(directory, file), "->", os.path.join(directory, new_name))
            base_number += 1

    return err_tag

# # 替换路径为你希望操作的目录
# rename_files_in_directory('E:\事务\上海\拍摄文件\原始数据\W11\W11A29\拼接图')
